package chances.epg.user.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import chances.epg.commons.utils.RequestUtils;
import chances.epg.entity.user.ScheduleOrder;
import chances.epg.user.service.ScheduleOrderService;

@RestController
@RequestMapping(path = "/subscr")
public class ScheduleOrderController extends UserAbstractController {

	@Autowired
	private ScheduleOrderService interactionService;

	@RequestMapping("/add")
	public UserResultResponse<String> add(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		ScheduleOrder userSub = this.createUsersubscr(request);
		String userId = request.getParameter("userId");
		String stbNo = request.getParameter("stbNo");
		String prog = request.getParameter("prog");
		userSub.setUserId(userId);
		userSub.setStbNo(stbNo);
		userSub.setExecStatus(ScheduleOrder.EXEC_STATUS_NO);
		userSub.setProg(prog);
		UserResultResponse<String> result = new UserResultResponse<String>();
		try {
			this.interactionService.add(userSub);
			result.setStatus(SUCCESS);
		} catch (Throwable ex) {
			result.setStatus(DB_ERROR);
		}
		return result;
	}

	@RequestMapping("/remove")
	public UserResultResponse<String> remove(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		String userId = request.getParameter("userId");
		String code = request.getParameter("code");
		UserResultResponse<String> result = new UserResultResponse<String>();
		try {
			this.interactionService.cancel(userId, code);
			result.setStatus(SUCCESS);
		} catch (Throwable ex) {
			result.setStatus(DB_ERROR);
		}
		return result;

	}

	@RequestMapping("/list")
	public UserResultSetResponse<ScheduleOrder> list(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		
		String userId = request.getParameter("userId");
		String stbNo = request.getParameter("stbNo");
		UserResultSetResponse<ScheduleOrder> result = new UserResultSetResponse<ScheduleOrder>();
		List<ScheduleOrder> subscriptions = new ArrayList<ScheduleOrder>();
		try {
			subscriptions = this.interactionService.listScheduleOrder(userId, stbNo);
			result.setResultSet(subscriptions);
			result.setStatus(SUCCESS);
		} catch (Throwable ex) {
			result.setStatus(DB_ERROR);
		}
		result.setResultSet(subscriptions);
		return result;
	}

	@RequestMapping("/dels")
	public UserResultResponse<String> dels(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		
		String userId = request.getParameter("userId");
		String ids = request.getParameter("codes");
		String stbNo = request.getParameter("stbNo");
		UserResultResponse<String> result = new UserResultResponse<String>();
		try {
			if (StringUtils.isNotEmpty(ids)) {
				String[] codes = ids.split(",");
				this.interactionService.deleteSub(userId, codes, stbNo);
			}
			result.setStatus(SUCCESS);
		} catch (Throwable ex) {
			result.setStatus(DB_ERROR);
		}
		return result;
	}
	
	private ScheduleOrder createUsersubscr(HttpServletRequest request) {
		ScheduleOrder userSub = new ScheduleOrder();
		userSub.setCode(RequestUtils.getParameter(request, "code", ""));
		userSub.setType(RequestUtils.getParameter(request, "type", ""));
		userSub.setTitle(RequestUtils.getParameter(request, "title", ""));
		userSub.setProg(RequestUtils.getParameter(request, "prog", ""));
		userSub.setIcon(RequestUtils.getParameter(request, "icon", ""));
		userSub.setNextDate(RequestUtils.getParameter(request, "nextDate", ""));
		userSub.setEndDate(RequestUtils.getParameter(request, "endDate", ""));
		return userSub;
	}

}
